home *** CD-ROM | disk | FTP | other *** search
- /*
- Harvest C
- Copyright 1992 Eric W. Sink. All rights reserved.
-
- This file is part of Harvest C.
-
- Harvest C is free software; you can redistribute it and/or modify
- it under the terms of the GNU Generic Public License as published by
- the Free Software Foundation; either version 2, or (at your option)
- any later version.
-
- Harvest C is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with Harvest C; see the file COPYING. If not, write to
- the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
-
- Harvest C is not in any way a product of the Free Software Foundation.
- Harvest C is not GNU software.
- Harvest C is not public domain.
-
- This file may have other copyrights which are applicable as well.
-
- */
-
- /*
- DTokenStream.c
-
- */
-
- #include "DTokenStream.h"
-
- void DTokenStream::ITokenStream(DHashTable *defines)
- {
- theDefines = defines;
- theTokens = new CList;
- theTokens->IList();
- collectorStack = new CStack;
- collectorStack->IStack();
- topCollector = NULL;
- parenLevel = 0;
- }
-
- long DTokenStream::GetNumItems(void)
- {
- return theTokens->GetNumItems();
- }
-
- DToken *DTokenStream::GetNthToken(long n)
- {
- return (DToken *) theTokens->NthItem(n);
- }
-
- void DTokenStream::PushCollector(void)
- {
- collectorStack->Push((void *) topCollector);
- }
-
- void DTokenStream::StartCollecting(DMacroCollector *aCollector)
- {
- topCollector = aCollector;
- topCollector->startingParenLevel = parenLevel;
- }
-
- void DTokenStream::RawAdd(DToken *theToken)
- {
- theTokens->Append((void *) theToken);
- }
-
- Boolean DTokenStream::IsEmpty(void)
- {
- return (theTokens->IsEmpty());
- }
-
- Boolean DTokenStream::Add(DToken *theToken,CSymbolList *beingExpanded)
- {
- DDefine *ppSymbol;
-
- if (theToken->IsExpandable())
- {
- if (theDefines)
- {
- if (ppSymbol = (DDefine *) theDefines->Find(theToken->name))
- {
- /* We've found a symbol with a preproc definition. We first
- check to see if it is already being expanded */
- if (beingExpanded)
- {
- if (beingExpanded->Find(theToken->name))
- {
- /* Found it - no recursive expansion */
- if (topCollector)
- {
- topCollector->AddToArg(theToken);
- }
- else
- {
- RawAdd(theToken);
- }
- }
- else
- {
- ppSymbol->Expand(this,beingExpanded);
- }
- }
- else
- {
- ppSymbol->Expand(this,beingExpanded);
- }
- }
- else
- {
- if (topCollector)
- {
- topCollector->AddToArg(theToken);
- }
- else
- {
- RawAdd(theToken);
- }
- }
- }
- else
- {
- /* Then this is not an expanding stream */
- RawAdd(theToken);
- }
- }
- else if (theToken->code == '(')
- {
- parenLevel++;
- if (topCollector)
- {
- if (parenLevel == (topCollector->startingParenLevel + 1))
- {
- /* Then we have just started fetching args */
- topCollector->StartNextArg();
- }
- else
- {
- topCollector->AddToArg(theToken);
- }
- }
- else
- {
- RawAdd(theToken);
- }
- }
- else if (theToken->code == ',')
- {
- if (topCollector)
- {
- if (parenLevel == (topCollector->startingParenLevel + 1))
- {
- /* Then we have just completed fetching one arg */
- topCollector->StartNextArg();
- }
- else
- {
- topCollector->AddToArg(theToken);
- }
- }
- else
- {
- RawAdd(theToken);
- }
- }
- else if (theToken->code == ')')
- {
- parenLevel--;
- if (topCollector)
- {
- if (parenLevel == (topCollector->startingParenLevel))
- {
- /* Then we have just completed fetching all the args */
- topCollector->Expand(this,beingExpanded);
- }
- else
- {
- topCollector->AddToArg(theToken);
- }
- }
- else
- {
- RawAdd(theToken);
- }
- }
- else
- {
- if (topCollector)
- {
- topCollector->AddToArg(theToken);
- }
- else
- {
- RawAdd(theToken);
- }
- }
- }
-
- void DTokenStream::Dispose(void)
- {
- theTokens->Dispose();
- collectorStack->Dispose();
- inherited::Dispose();
- }
-
-